/**
 * 
 * <p>
 * https://www.top.iridescent
 * <p>
 * 版权所有，侵权必究！
 */

package top.iridescent.generate.config;

import top.iridescent.exception.model.RRException;
import top.iridescent.generate.dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * 数据库配置
 *
 * @author 
 */
@Configuration
public class DbConfig {
    @Value("${renren.database:mysql}")
    private String database;
    @Autowired
    private MySQLGeneratorDao mySQLGeneratorDao;
    @Autowired
    private OracleGeneratorDao oracleGeneratorDao;
    @Autowired
    private SQLServerGeneratorDao sqlServerGeneratorDao;
    @Autowired
    private PostgreSQLGeneratorDao postgreSQLGeneratorDao;

    private static boolean mongo = false;

    public  GeneratorDao getByBbType(String dbType){
        if ("mysql".equalsIgnoreCase(dbType)) {
            return mySQLGeneratorDao;
        } else if ("oracle".equalsIgnoreCase(dbType)) {
            return oracleGeneratorDao;
        } else if ("sqlserver".equalsIgnoreCase(dbType)) {
            return sqlServerGeneratorDao;
        } else if ("postgresql".equalsIgnoreCase(dbType)) {
            return postgreSQLGeneratorDao;
        } else {
            throw new RRException("不支持当前数据库：" + dbType);
        }
    }

    @Bean
    @Primary
    public GeneratorDao getGeneratorDao() {
        if ("mysql".equalsIgnoreCase(database)) {
            return mySQLGeneratorDao;
        } else if ("oracle".equalsIgnoreCase(database)) {
            return oracleGeneratorDao;
        } else if ("sqlserver".equalsIgnoreCase(database)) {
            return sqlServerGeneratorDao;
        } else if ("postgresql".equalsIgnoreCase(database)) {
            return postgreSQLGeneratorDao;
        } else {
            throw new RRException("不支持当前数据库：" + database);
        }
    }


    public static boolean isMongo() {
        return mongo;
    }

}
